package com.pkche.core.base.converter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;

/**
 * @FileName      : DateConverter.java
 * @Author        : pq
 * @Create Date   : 2012-10-12
 * @Last Modified :   
 * @Description   : 日期转换
 */
public class DateConverter implements Converter<String , Date> {

	private List<String> formats = new ArrayList(){{this.add("yyyy-MM-dd HH:mm:ss");
													this.add("yyyy-MM-dd");}};
	
	public Date convert(String source) {
		Iterator<String> iter = formats.iterator();
        if (StringUtils.isBlank(source)) {
        	return null;
        }
        else {
        	while (iter.hasNext()) {
        		String format = iter.next();
        		try {
        			SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        			return dateFormat.parse(source);
        		}catch(Exception e){
        			e.printStackTrace();
        		}
        	}
        }
        return null;
	}

}
